package org.naxx.security.naxxlet.model.combiner;

import org.naxx.security.common.Decision;
import org.naxx.security.model.Condition;
import org.naxx.security.model.Rule;
import org.naxx.security.model.Target;
import org.naxx.security.model.Condition.ConditionResult;
import org.naxx.security.model.request.Request;
import org.naxx.security.model.result.TargetEvaluationResult;

/**
 * This class evaluates one rule against request context. It is
 * used by <i>RulesCombiner</i>.
 */
final class RuleEvaluator {

	Decision evaluate(Rule rule, Request request) {
		Target target = rule.getTarget();
		TargetEvaluationResult targetResult = TargetEvaluationResult.MATCH;
		if(target != null) {
			targetResult = null;//target.evaluate(requestContext);
		}
		if(TargetEvaluationResult.NO_MATCH.equals(targetResult)) {
			return Decision.NOT_APPLICABLE;
		}
		if(TargetEvaluationResult.INDETERMINATE.equals(targetResult)) {
			return Decision.INDETERMINATE;
		}
		Condition condition = rule.getCondition();
		if(condition == null) {
			return rule.getEffect();
		}
		ConditionResult conditionResult = null;//condition.evaluate(requestContext);
		if(ConditionResult.FALSE.equals(conditionResult)) {
			return Decision.NOT_APPLICABLE;
		}
		else if(ConditionResult.INDETERMINATE.equals(conditionResult)) {
			return Decision.INDETERMINATE;
		}
		return rule.getEffect();
	}
}
